package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_bankcard;
import cn.com.api.entity.ApiT_member;
import cn.com.api.mapper.ApiT_bankcardMapper;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.service.ApiT_bankcardService;
import cn.com.api.service.impl.base.T_bankcardServiceImplBase;
import cn.com.juhe.JuheConfig;
import cn.com.juhe.JuheUtils;
import cn.com.util.CipherUtil;
import cn.com.util.DateUtil;
import cn.com.util.Utils;
import net.sf.json.JSONObject;

/**
 * 
 **/
@Service
public class ApiT_bankcardServiceImpl extends T_bankcardServiceImplBase implements ApiT_bankcardService {
	@Autowired
	private ApiT_bankcardMapper apiT_bankcardMapper;
	@Autowired
	private ApiT_memberMapper apiT_memberMapper;

	public List<ApiT_bankcard> getmybankcards(String user_id) {
		return apiT_bankcardMapper.getmybankcards(user_id);
	}

	public Map<String, Object> addbankcard(ApiT_bankcard apiT_bankcard, String password) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ApiT_member apiT_member = apiT_memberMapper.findT_memberById(Integer.valueOf(apiT_bankcard.getU_id()));
		if (apiT_member.getPay_pwd() != null && !"".equals(apiT_member.getPay_pwd())) {
			if (CipherUtil.generatePassword(password).equals(apiT_member.getPay_pwd())) {
				Boolean flag = true;
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("key", JuheConfig.verifybankcardkey);
				data.put("idcard", apiT_bankcard.getCredentialsno());
				data.put("realname", apiT_bankcard.getCardname());
				data.put("bankcard", apiT_bankcard.getCardno());
				data.put("mobile", apiT_bankcard.getPhone());
				String result = JuheUtils.net(JuheConfig.verifybankcard4Url, data, "GET");
				JSONObject json = JSONObject.fromObject(result);
				Integer error_code = json.getInt("error_code");
				if (error_code == 0) {
					Integer res = json.getJSONObject("result").getInt("res");
					if (res != 1) {
						flag = false;
						String message = json.getJSONObject("result").getString("message");
						map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						map.put(Utils.MESSAGE, message);
					}
				} else {
					flag = false;
					String reason = json.getString("reason");
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, reason);
				}
				if (flag) {
					apiT_bankcard.setCreate_time(DateUtil.nowTimeToString());
					apiT_bankcardMapper.bankcardInsertion(apiT_bankcard);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "添加成功");
					map.put(Utils.DATAS, apiT_bankcard);
				}
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "支付密码不正确");
			}
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "请先设置支付密码");
		}
		return map;
	}

	// 获取银行卡详情
	public Map<String, Object> getBankcardInfo(String cardno) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("key", JuheConfig.detectionBankCardkey);
		data.put("cardid", cardno);
		String result = JuheUtils.net(JuheConfig.detectionBankCardUrl, data, "GET");
		JSONObject json = JSONObject.fromObject(result);
		Integer error_code = json.getInt("error_code");
		if (error_code == 0) {
			JSONObject detection = json.getJSONObject("result");
			data.clear();
			data.put("kefu", detection.getString("kefu"));
			data.put("logo", detection.getString("logo"));
			data.put("nature", detection.getString("nature"));
			data.put("bank", detection.getString("bank"));
			data.put("type", detection.getString("type"));
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, data);
		} else {
			String reason = json.getString("reason");
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, reason);
		}
		return map;
	}
}
